package com.auxgroup.smarthome.inner.service;

import com.auxgroup.bridge.app.inner.vo.DeviceFaultVo;
import com.auxgroup.smarthome.BeanListUtils;
import com.auxgroup.smarthome.BeanUtils;
import com.auxgroup.smarthome.appconst.BusinessConstant;
import com.auxgroup.smarthome.inner.dto.DeviceFaultDto;
import com.auxgroup.smarthome.inner.entity.DeviceFaultEntity;
import com.auxgroup.smarthome.inner.repo.DeviceFaultRepo;
import com.auxgroup.smarthome.utils.PageContent;
import com.auxgroup.smarthome.utils.PageUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lixiaoxiao on 17-8-7.
 */
@Service
public class DeviceFaultService {
    @Autowired
    private DeviceFaultRepo deviceFaultRepo;

    /**
     * 保存设备故障信息
     *
     * @param deviceFaultDto
     * @return
     */
    public boolean saveDeviceFault(DeviceFaultDto deviceFaultDto) {
        DeviceFaultEntity deviceFaultEntity = new DeviceFaultEntity();
        BeanUtils.copyAttrs(deviceFaultEntity, deviceFaultDto);
        deviceFaultRepo.save(deviceFaultEntity);
        return true;
    }

    /**
     * 根据mac、username查找未解决的故障
     * @param mac
     * @return
     */
    public DeviceFaultDto findDeviceFault(String mac){
        int status = BusinessConstant.FAULT_NOT_RESOLVED.getValue();
        DeviceFaultEntity deviceFaultEntity = deviceFaultRepo.findByMacAndStatus(mac,status);
        return BeanUtils.copyAttrs(new DeviceFaultDto(),deviceFaultEntity);
    }

    /**
     * app查询故障列表
     * 添加规则 获取最新的10条错误 modify by lixiaoxiao 2017/9/8
     * @param mac
     * @return
     */
    public List<DeviceFaultVo> findAppDeviceFaultList(String mac) {
        List<DeviceFaultEntity> deviceFaultEntities = deviceFaultRepo.findByMac(mac,0,10);
        BeanListUtils beanListUtils = new BeanListUtils(DeviceFaultVo.class,deviceFaultEntities);
        return beanListUtils.getTargetList();
    }

    public List<DeviceFaultVo> findAppDeviceHisFaultList(String uid) {
        List<DeviceFaultEntity> deviceFaultEntities = deviceFaultRepo.findByUid(uid);
        BeanListUtils beanListUtils = new BeanListUtils(DeviceFaultVo.class,deviceFaultEntities);
        return beanListUtils.getTargetList();
    }
    /**
     * 查询设备故障列表
     *
     * @param keyword
     * @param page
     * @param size
     * @return
     */
    public PageContent<DeviceFaultVo> findDeviceFaultList(String keyword, int page, int size, int status) {
        Sort sort = new Sort(Sort.Direction.DESC, "occurrenceTime");
        Pageable pageable = new PageRequest(page, size, sort);
        Page<DeviceFaultEntity> deviceFaultEntities = deviceFaultRepo.findAll(getWhereClause(keyword,status), pageable);
        Page<DeviceFaultVo> deviceFaultVos = new PageUtils(DeviceFaultVo.class, deviceFaultEntities).buildVosPage();
        return new PageContent<DeviceFaultVo>(deviceFaultVos);
    }

    /**
     * 配置动态查询条件
     *
     * @return
     */
    public Specification<DeviceFaultEntity> getWhereClause(String keyword,int status) {
        return new Specification<DeviceFaultEntity>() {
            @Override
            public Predicate toPredicate(Root<DeviceFaultEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (StringUtils.isNotBlank(keyword)) {
                    String inKeyword = "%" + keyword + "%";
                    Predicate predicate = cb.or(cb.like(root.get("mac").as(String.class), inKeyword),
                            cb.like(root.get("username").as(String.class), inKeyword),
                            cb.like(root.get("phone").as(String.class), inKeyword));
                    list.add(predicate);
                }

                list.add(cb.equal(root.get("status").as(Integer.class), status));
                return cb.and(list.toArray(new Predicate[list.size()]));
            }
        };
    }
}